/***************************************************************************
 *   oboe.utils.TexturePool                                                *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.utils
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	
	public class TexturePool extends BitmapData
	{
		private var textureGridSize:int;
		private var textureTileSize:int;

		public function TexturePool(size:int, textureCount:int, transparent:Boolean=true, fillColor:uint=4.294967295E9)
		{
			this.textureGridSize = 1;
			while( this.textureGridSize*this.textureGridSize < textureCount )
			{
				++this.textureGridSize;
			}
			
			this.textureTileSize = size/this.textureGridSize;

			super( size, size, transparent, fillColor);
		}
		
		private static const tmatrix:Matrix = new Matrix();
		public function setTexture( texture:DisplayObject, id:int ):void
		{
			var w:Number = (this.width-1)/this.textureGridSize;
			tmatrix.identity();
			var ty:int = int( id/this.textureGridSize );
			var tx:int = id%this.textureGridSize;
			tmatrix.ty = ty*this.textureTileSize;
			tmatrix.tx = tx*this.textureTileSize;
			tmatrix.d  = tmatrix.a  = this.textureTileSize/texture.width;
			this.textureTileSize/texture.height;
			this.draw( texture, tmatrix, null, null, null, true );
		}
	}
}